using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using MySQLStudio.BusinessLogic;
using MySQLStudio.Common;

namespace MySQLStudio.GUI.Controls
{
    public enum RoutineFormType
    {
        CREATE_ROUTINE,
        ALTER_ROUTINE
    }

    public partial class RoutineActivitiesForm : Telerik.WinControls.UI.RadForm
    {                                                         
        private RoutineFormType m_formType;        
        private StoredProcedure m_storedProcedure;
        private Host m_host;
        private Database m_database;        

        public RoutineFormType FormType
        {
            get { return m_formType; }
            set { m_formType = value; }
        }

        public StoredProcedure StoredProcedure
        {
            get { return m_storedProcedure; }
            set { m_storedProcedure = value; }
        }

        public Host Host
        {
            get { return m_host; }
            set { m_host = value; }
        }

        public Database Database
        {
            get { return m_database; }
            set { m_database = value; }
        }
        
       
        public RoutineActivitiesForm()
        {
            InitializeComponent();

            tbDDL.Seperators.Add(' ');
            tbDDL.Seperators.Add('\r');
            tbDDL.Seperators.Add('\n');
            tbDDL.Seperators.Add(',');
            tbDDL.Seperators.Add('.');
            tbDDL.Seperators.Add('-');
            tbDDL.Seperators.Add('+');
           
            tbDDL.HighlightDescriptors = Highlighters.Sql;
        }

        public void ShowAlterRoutineStatement()
        {
            if (this.m_storedProcedure != null)
            {
                tbName.Text = this.m_storedProcedure.Name;
                tbDDL.Text = this.m_storedProcedure.GetAlterStatement(this.m_host.ConnectionString);
                tbDDL.OnTextChanged();
            }
        }

        public void ShowCreateNewRoutineStatement()
        {
            tbName.Text = "new_routine";
            tbDDL.Text = InformationSchemaHelper.GetCreateStoredProcedureStatementCommand();
            tbDDL.OnTextChanged();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.m_host != null && this.m_database != null)
                {
                    if (!string.IsNullOrEmpty(tbDDL.Text))                                           
                        RoutinesController.CreateStoredProcedure(this.m_host.ConnectionString, GenerateCreateRoutineStatement(this.m_database, tbDDL.Text));                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(MySQLStudio.Common.Logger.GetExceptionFullContent(ex));
            }
        }

        private string GenerateCreateRoutineStatement(Database databaseContext, string createRoutineSQL)
        {
            return string.Format("USE {0};\n{1}", databaseContext.Name, createRoutineSQL);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
